const mongoose = require('mongoose');
var User = require('../models/use.model');
//插入数据
exports.create = function(req, res, next) {
    const user = new User(req.body);
    user.save((err,product) => {
        if(!err){
            console.log('插入数据成功')
        }
        res.end();
    })
}
//输出所有数据
exports.find = function(req,res,next){
    User.find({},(err,docs) => {
        if(!err){
            res.send(docs);
        }
    });
}
//修改数据
exports.update = function(req,res,next){
    const id = req.params.id;
    User.findByIdAndUpdate(id,{$set: req.body},{new:false}).then(data=>{
    res.json(data);
    })
}
  //删除数据
exports.remove = function(req,res,next){
    const id = req.params.id;
    User.deleteOne(id,(err,data)=>{
        res.json(data);
    })
}
//查找指定的数据
exports.get = function(req,res,next){
    const id = req.params.id;
    User.findById(id).then(data=>{
      res.json(data);
    })
  }
//分页数据
exports.list = function(req,res,next){
    var page  = req.body.page ? req.body.page : 1;
    var limit = req.body.limit ? req.body.limit : 10;

    User.paginate({}, { page: +page, limit: +limit }, function(err, result) {
        res.json(result);
    });
}



